/*
箭头函数没有自己的this。与ES5函数不同(ES5函数将创建自己的单独执行上下文)，
箭头函数使用包含自己的执行上下文
*/
var shoppingCart = {
    items: ['Apple', 'Orange'],
    inventory: {Apple: 1, Orange: 0},
    checkout (){
        this.items.forEach(item => {
            if(!this.inventory[item]) {
                console.log('this: ', this);
                console.log('Item ' + item + ' has sold out.');
            }
        })
    }
};
shoppingCart.checkout();

//对应的ES5
var shoppingCartEs5 = {
    items: ['Apple', 'Orange'],
    inventory: {Apple: 1, Orange: 0},
    checkout (){
        //重新定义一个变量
        //checkout范围内的this赋值给that
        var that = this;
        this.items.forEach(function(item) {
            //这里用that 其实是checkout中的this
            if(!that.inventory[item]) {
                console.log('this: ', this);  //这个this是global
                console.log('that: ', that);
                console.log('Item ' + item + ' has sold out.');
            }
        })
    }
};
shoppingCartEs5.checkout();

